CKEDITOR.dialog.add('smartchart', function(editor) {
	var id;
	return {
		title: 'Chart管理',
		minWidth: 750,
		minHeight: 550,
		onOk: function() {
			SmartChart.okSign = true;
			if (SmartChart.okSign) {
				SmartChart.okSign = false;
				var dialog = this;
				setTimeout(function() {
					var target = dialog.getSelectedElement().findOne('.simplebox-content').$;
					if ($.isEmptyObject(SmartChart.option)) {
						var code = dialog.getContentElement('tab-basic', 'code').getValue();
						var xPoint = dialog.getContentElement('tab-basic', 'xPoint').getValue();
						var yPoint = dialog.getContentElement('tab-basic', 'yPoint').getValue();
						var yPoint2 = dialog.getContentElement('tab-basic', 'yPoint2').getValue();
						var chartType = dialog.getContentElement('tab-basic', 'chartType').getValue();

						SmartChart.ajaxReqCharData(code, xPoint, yPoint, yPoint2, $(target), chartType);
					} else {
						SmartChart.buildChart(SmartChart.option, 'insertChart', target);
					}
					$(dialog.getSelectedElement().findOne('.' + SmartChart.cls).$).data(id, SmartChart.option);
				}, 50);
			}
		},
		onShow: function() {
			SmartChart.option = {};

			// 去除chart 图
			$('#previewChart').children().remove();

			var editor = this.getParentEditor(),
				sel = editor.getSelection(),
				element = sel && sel.getSelectedElement();

			if (element) {
				var target = element.findOne('.' + SmartChart.cls);
				if (target) {
					id = target.getAttribute('id');
					var option = $(target.$).data(id);
					SmartChart.buildChart(option, 'insertChart', $('#previewChart')[0]);
				}
			}
		},
		contents: [{
			id: 'tab-basic',
			elements: [{
				type: 'hbox',
				widths: ['1%', '1%', '1%', '1%', '50%'],
				children: [{
					type: 'select',
					id: 'chartType',
					label: '类型选择',
					align: 'left',
					width: '65px',
					items: [
						['过程线', 'line'],
						['柱状图', 'bar'],
						['双坐标过程线', 'doubleLine']
					],
					setup: function(widget) {
						var chartType = widget.element.getAttribute('charttype');
						if (chartType) {
							this.setValue(chartType);
						} else {
							this.setValue('line');
						}
					},
					commit: function(widget) {
						var element = widget.element;
						element.setAttribute('charttype', this.getValue());

						// 生成id
						id = element.getAttribute('id');
						if (!id) {
							id = 'chart_' + new Date().getTime();
							element.setAttribute('id', id);
						}
					},
					onChange: function() {
						var val = this.getValue();

						var dialog = this.getDialog();
						var yPoint2ConEle = dialog.getContentElement('tab-basic', 'yPoint2');

						if (val == 'doubleLine') {
							yPoint2ConEle.getElement().show();
						} else {
							yPoint2ConEle.getElement().hide();
						}
					}
				}, {
					type: 'text',
					id: 'xPoint',
					width: '60px',
					label: 'X轴坐标',
					setup: function(widget) {
						var xPoint = widget.element.getAttribute('xpoint');
						this.setValue(xPoint);
					},
					commit: function(widget) {
						widget.element.setAttribute('xpoint', this.getValue());
					}
				}, {
					type: 'text',
					id: 'yPoint',
					width: '60px',
					label: 'Y轴坐标',
					setup: function(widget) {
						var yPoint = widget.element.getAttribute('ypoint');
						this.setValue(yPoint);
					},
					commit: function(widget) {
						widget.element.setAttribute('ypoint', this.getValue());
					}
				}, {
					type: 'text',
					id: 'yPoint2',
					width: '60px',
					style: 'display:none;',
					label: 'Y2轴坐标',
					setup: function(widget) {
						var yPoint2 = widget.element.getAttribute('ypoint2');
						this.setValue(yPoint2);
					},
					commit: function(widget) {
						widget.element.setAttribute('ypoint2', this.getValue());
					}
				}, {
					type: 'text',
					style: 'display:none;'
				}]
			}, {
				type: 'hbox',
				widths: ['80%', '20%'],
				children: [{
					type: 'text',
					id: 'code',
					label: '脚本',
					validate: CKEDITOR.dialog.validate.notEmpty("脚本不能为空"),
					setup: function(widget) {
						var code = widget.element.getAttribute('code');
						this.setValue(code);
					},
					commit: function(widget) {
						widget.element.setAttribute('code', this.getValue());
					}
				}, {
					id: 'preview',
					type: 'button',
					label: '预览',
					style: 'margin-top:14px',
					onClick: function() {
						var dialog = this.getDialog();
						var code = dialog.getContentElement('tab-basic', 'code').getValue();
						var xPoint = dialog.getContentElement('tab-basic', 'xPoint').getValue();
						var yPoint = dialog.getContentElement('tab-basic', 'yPoint').getValue();
						var yPoint2 = dialog.getContentElement('tab-basic', 'yPoint2').getValue();
						var chartType = dialog.getContentElement('tab-basic', 'chartType').getValue();

						SmartChart.ajaxReqCharData(code, xPoint, yPoint, yPoint2, $('#previewChart'), chartType);
					}
				}]
			}, {
				type: 'html',
				html: '<div id="preview"><b>预览:</b><br/><div id="previewChart" style="height:400px;border:1px solid rgb(138, 134, 134);"></div></div>'
			}]
		}]
	};
});